package org.i_plus.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.i_plus.model.MemberBean;
import org.i_plus.model.services.MemberService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


@WebServlet("/CheckAccount")
public class CheckAccount extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MemberService service;
	public void init() throws ServletException {
		WebApplicationContext context =
				WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
		this.service = (MemberService) context.getBean("MemberService");		
	}
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String temp1=request.getParameter("account");
		response.setCharacterEncoding("UTF-8");
		if(temp1==null || temp1.length()==0)
		{
			response.getWriter().println("請輸入帳號，必須是E-mail");
			return;
		}
			MemberBean mbean=service.select_by_account(temp1);
			if(mbean!=null)
			{
			response.getWriter().println("帳號已使用");
			response.getWriter().close();
			return;
			}else{
			response.getWriter().println("帳號未使用");
			response.getWriter().close();
			return;
			}
		
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
